package com.hst.capacity.application.biz.impl;

import cn.smartravel.traffic.alitrip.AlitripCarOpen;
import cn.smartravel.traffic.usoft17.RideSupplierCallback;
import cn.smartravel.traffic.usoft17.model.request.callback.OrderStatusDTO;
import cn.smartravel.traffic.usoft17.model.response.callback.ResultVO;
import com.alibaba.fastjson2.JSON;
import com.hst.capacity.application.biz.*;
import com.hst.capacity.domain.common.constants.*;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.entity.driver.DriverEntity;
import com.hst.capacity.domain.model.entity.franchisee.customer.CustomerFranchiseeEntity;
import com.hst.capacity.domain.model.entity.order.base.BaseOrderEntity;
import com.hst.capacity.domain.model.entity.order.express.ExpressOrderEntity;
import com.hst.capacity.domain.model.entity.order.express.ExpressOrderOperationHistory;
import com.hst.capacity.domain.model.entity.order.express.ExpressOrderStatusHistoryEntity;
import com.hst.capacity.domain.model.entity.order.intercity.IntercityOrderEntity;
import com.hst.capacity.domain.model.entity.order.intercity.IntercityOrderOperationHistory;
import com.hst.capacity.domain.model.entity.order.intercity.IntercityOrderStatusHistoryEntity;
import com.hst.capacity.domain.model.entity.order.pool.OrderPoolEntity;
import com.hst.capacity.domain.model.entity.order.specialcar.SpCarOrderOperationHistory;
import com.hst.capacity.domain.model.entity.order.specialcar.SpCarOrderStatusHistoryEntity;
import com.hst.capacity.domain.model.request.OrderForDispatchParam;
import com.hst.capacity.domain.model.request.OrderParam;
import com.hst.capacity.domain.model.request.dispatch.DispatchDriverParam;
import com.hst.capacity.domain.model.request.dispatch.DispatchParam;
import com.hst.capacity.domain.model.request.dispatch.DispatchPartyDriverParam;
import com.hst.capacity.domain.model.request.dispatcher.DispatcherOrderParam;
import com.hst.capacity.domain.model.request.export.OrderExportParam;
import com.hst.capacity.domain.model.response.PagingResultVO;
import com.hst.capacity.domain.model.response.driver.DriverInfoVO;
import com.hst.capacity.domain.model.response.export.ExportOrderVO;
import com.hst.capacity.domain.model.response.order.OrderDetailVO;
import com.hst.capacity.domain.model.response.order.OrderForDispatchVO;
import com.hst.capacity.domain.model.response.order.OrderOperationLogVO;
import com.hst.capacity.domain.model.response.order.OrderVO;
import com.hst.capacity.domain.repository.redis.CacheRedis;
import com.hst.capacity.domain.repository.redis.NotificationRedis;
import com.hst.capacity.domain.service.*;
import com.hst.capacity.infra.convertor.DriverConvertor;
import com.hst.capacity.infra.convertor.OrderConvertor;
import com.hst.capacity.infra.persistence.jpa.franchisee.customer.CustomerFranchiseeRepository;
import com.hst.capacity.infra.persistence.jpa.order.express.ExOrderOperationHistoryRepository;
import com.hst.capacity.infra.persistence.jpa.order.express.ExOrderRepository;
import com.hst.capacity.infra.persistence.jpa.order.express.ExOrderStatusHistoryRepository;
import com.hst.capacity.infra.persistence.jpa.order.intercity.IcOrderOperationHistoryRepository;
import com.hst.capacity.infra.persistence.jpa.order.intercity.IcOrderRepository;
import com.hst.capacity.infra.persistence.jpa.order.intercity.IcOrderStatusHistoryRepository;
import com.hst.capacity.infra.persistence.jpa.order.pool.OrderPoolRepository;
import com.hst.capacity.infra.persistence.jpa.order.specialcar.SpCarOrderOperationHistoryRepository;
import com.hst.capacity.infra.persistence.jpa.order.specialcar.SpOrderStatusHistoryRepository;
import com.hst.capacity.infra.persistence.jpa.setting.commision.FranchiseeCommisionRepository;
import com.hst.capacity.infra.persistence.mybatis.OrderMapper;
import com.hst.capacity.infra.util.DateUtil;
import com.hst.capacity.infra.util.StringUtil;
import com.taobao.api.request.AlitripCarOrderCompleteRequest;
import com.taobao.api.response.AlitripCarOrderCompleteResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@Slf4j
@Service
public class OrderBizImpl implements OrderBiz {

    @Autowired
    ExpressOrderBiz exOrderBiz;
    @Autowired
    IntercityOrderBiz icOrderBiz;
    @Autowired
    OrderPoolBiz orderPoolBiz;
    @Autowired
    FranchiseeBiz franchiseeBiz;

    @Autowired
    OrderPoolRepository orderPoolRepository;
    @Autowired
    IcOrderRepository icOrderRepository;
    @Autowired
    ExOrderRepository exOrderRepository;

    @Autowired
    ExOrderOperationHistoryRepository exOrderOperationHistoryRepository;
    @Autowired
    IcOrderOperationHistoryRepository icOrderOperationHistoryRepository;
    @Autowired
    SpCarOrderOperationHistoryRepository orderOperationHistoryRepository;

    @Autowired
    IcOrderStatusHistoryRepository icOrderStatusHistoryRepository;
    @Autowired
    ExOrderStatusHistoryRepository exOrderStatusHistoryRepository;
    @Autowired
    SpOrderStatusHistoryRepository orderStatusHistoryRepository;
    @Autowired
    FranchiseeCommisionRepository fcRepository;
    @Autowired
    CustomerFranchiseeRepository customerFranchiseeRepository;

    @Autowired
    JobPlatformBiz jobService;
    @Autowired
    AlitripCarOpen alitripCarOpen;
    @Autowired
    RideSupplierCallback callback;
    @Autowired
    DriverService driverService;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    ExpressService expressService;
    @Autowired
    IntercityService intercityService;
    @Autowired
    OrderPoolService orderPoolService;
    @Autowired
    AuthService authService;
    @Autowired
    CacheRedis cacheRedis;
    @Autowired
    NotificationRedis notificationRedis;

    @Override
    public OrderVO refresh(OrderParam param) {
        OrderVO orderVO = null;
        switch (param.getModuleType()) {
            case Constants.OrderModuleType.SPECIAL:
                orderVO = orderPoolBiz.refresh(param);
                break;
            case Constants.OrderModuleType.INTERCITY:
                orderVO = icOrderBiz.refresh(param);
                break;
            case Constants.OrderModuleType.EXPRESS:
                orderVO = exOrderBiz.refresh(param);
                break;
        }
        return orderVO;
    }

    /**
     * 司机改派
     * @param param
     * @return
     */
    @Override
    public Boolean changeDriver(DispatchDriverParam param) {
        boolean result = false;
        switch (param.getModuleType()) {
            case Constants.OrderModuleType.SPECIAL:
                result = orderPoolBiz.reDispatchDriver(param);
                break;
            case Constants.OrderModuleType.INTERCITY:
                result = icOrderBiz.reDispatchDriver(param);
                break;
            case Constants.OrderModuleType.EXPRESS:
                result = exOrderBiz.reDispatchDriver(param);
                break;
        }
        return result;
    }

    /**
     * 司机派单
     * @param param
     * @return
     */
    @Transactional
    @Override
    public Boolean dispatchDriver(DispatchDriverParam param) {
        boolean result = false;
        int moduleType = StringUtil.orderType(param.getIdOrder());
        log.info("订单ID",param.getIdOrder());
        switch (moduleType) {
            case Constants.OrderModuleType.SPECIAL:
                OrderPoolEntity orderPool = orderPoolRepository.findByIdOrder(param.getIdOrder());
                if (orderPool == null)
                    throw new BusinessException(5002, "订单不存在，请检查参数idOrder是否正确");
                if (StringUtil.isNotBlank(orderPool.getIdDriver())) {
                    result = orderPoolBiz.reDispatchDriver(param);
                }else{
                    result = orderPoolBiz.dispatchDriver(param);
                }
                break;
            case Constants.OrderModuleType.INTERCITY:
                IntercityOrderEntity orderIc = icOrderRepository.findByIdOrder(param.getIdOrder());
                if (orderIc == null)
                    throw new BusinessException(5002, "订单不存在，请检查参数idOrder是否正确");
                if (StringUtil.isNotBlank(orderIc.getIdDriver())) {
                    result = icOrderBiz.reDispatchDriver(param);
                }else{
                    result = icOrderBiz.dispatchDriver(param);
                }
                break;
            case Constants.OrderModuleType.EXPRESS:
                ExpressOrderEntity orderEx = exOrderRepository.findByIdOrder(param.getIdOrder());
                if (orderEx == null)
                    throw new BusinessException(5002, "订单不存在，请检查参数idOrder是否正确");
                if (StringUtil.isNotBlank(orderEx.getIdDriver())) {
                    result = exOrderBiz.reDispatchDriver(param);
                }else{
                    result = exOrderBiz.dispatchDriver(param);
                }
                break;
        }
        return result;
    }

    /**
     * 外部司机
     * @param param
     * @return
     */
    @Override
    public Boolean dispatchPartyDriver(DispatchPartyDriverParam param) {
        int moduleType = StringUtil.orderType(param.getIdOrder());
        boolean result = false;
        switch (moduleType) {
            case Constants.OrderModuleType.SPECIAL:
                result = orderPoolService.dispatchPartyDriver(param);
                break;
            case Constants.OrderModuleType.INTERCITY:
                result = intercityService.dispatchPartyDriver(param);
                break;
            case Constants.OrderModuleType.EXPRESS:
                result = expressService.dispatchPartyDriver(param);
                break;
        }
        return result;
    }

    @Override
    public OrderDetailVO getOrderDetail(String idOrder) {
        int moduleType = StringUtil.orderType(idOrder);
        OrderDetailVO orderDetail = new OrderDetailVO();
        orderDetail.setModuleType(moduleType);

        switch (moduleType) {
            case Constants.OrderModuleType.SPECIAL:
                OrderPoolEntity orderPool = orderPoolRepository.findByIdOrder(idOrder);
                if (orderPool == null)
                    throw new BusinessException(5002, "订单不存在，请检查参数idOrder是否正确");
                orderDetail = OrderConvertor.INSTANCE.convertToOrderDetail(orderPool);
                orderDetail.setLineName("-");
                break;
            case Constants.OrderModuleType.INTERCITY:
                IntercityOrderEntity icOrder = icOrderRepository.findByIdOrder(idOrder);
                if (icOrder == null){
                    throw new BusinessException(5002, "订单不存在，请检查参数idOrder是否正确");
                }
                orderDetail = OrderConvertor.INSTANCE.convertToOrderDetail(icOrder);
                orderDetail.setLineName(intercityService.findLineName(icOrder.getIdLine()));
                break;
            case Constants.OrderModuleType.EXPRESS:
                ExpressOrderEntity exOrder = exOrderRepository.findByIdOrder(idOrder);
                if (exOrder == null){
                    throw new BusinessException(5002, "订单不存在，请检查参数idOrder是否正确");
                }
                orderDetail = OrderConvertor.INSTANCE.convertToOrderDetail(exOrder);
                orderDetail.setLineName("-");
                break;
        }

        if (StringUtil.isBlank(orderDetail.getDriverInfo())){
            DriverInfoVO driverInfo = driverService.driverInfo(orderDetail.getIdDriver());
            if (driverInfo != null) {
                orderDetail.setDriverInfo(driverInfo.getRealName() + '/' + driverInfo.getDriverMobile());
            }
        }

        return orderDetail;
    }

    @Override
    public List<OrderOperationLogVO> getOrderOperationLogList(String idOrder) {
        int moduleType = StringUtil.orderType(idOrder);
        List<OrderOperationLogVO> list = new ArrayList<>();
        switch (moduleType) {
            case Constants.OrderModuleType.SPECIAL:
                List<SpCarOrderOperationHistory> orderList = orderOperationHistoryRepository.findByIdOrder(idOrder);
                list = OrderConvertor.INSTANCE.convertToOrderLogList(orderList);
                break;
            case Constants.OrderModuleType.INTERCITY:
                List<IntercityOrderOperationHistory> icOrderList = icOrderOperationHistoryRepository.findByIdOrder(idOrder);
                list = OrderConvertor.INSTANCE.convertToOrderLogListIc(icOrderList);
                break;
            case Constants.OrderModuleType.EXPRESS:
                List<ExpressOrderOperationHistory> exOrderList = exOrderOperationHistoryRepository.findByIdOrder(idOrder);
                list = OrderConvertor.INSTANCE.convertToOrderLogListEx(exOrderList);
                break;
        }
        return list;
    }

    @Override
    public Boolean changeDriverIncome(String idOrder,Float driverIncome) {
        boolean result = false;
        int moduleType = StringUtil.orderType(idOrder);
        switch (moduleType) {
            case Constants.OrderModuleType.SPECIAL:
                OrderPoolEntity orderPool = orderPoolRepository.findById(idOrder).orElse(null);
                if (orderPool == null)
                    throw new BusinessException(5002, "订单不存在，请检查参数idOrder是否正确");
                orderPool.setDriverIncome(driverIncome);
                orderPoolRepository.save(orderPool);
                result=true;
                break;
            case Constants.OrderModuleType.INTERCITY:
                IntercityOrderEntity orderIc = icOrderRepository.findById(idOrder).orElse(null);
                if (orderIc == null)
                    throw new BusinessException(5002, "订单不存在，请检查参数idOrder是否正确");
                orderIc.setDriverIncome(driverIncome);
                icOrderRepository.save(orderIc);
                result=true;
                break;
            case Constants.OrderModuleType.EXPRESS:
                ExpressOrderEntity orderEx = exOrderRepository.findById(idOrder).orElse(null);
                if (orderEx == null)
                    throw new BusinessException(5002, "订单不存在，请检查参数idOrder是否正确");
                orderEx.setDriverIncome(driverIncome);
                exOrderRepository.save(orderEx);
                result=true;
                break;
        }
        return result;
    }

    @Override
    public Boolean finishOrder(String idOrder) {
        boolean result = false;
        int moduleType = StringUtil.orderType(idOrder);
        List<OrderOperationLogVO> list = new ArrayList<>();
        int status = Constants.OrderStatus.ORDER_STATUS_END_SERVICE;
        switch (moduleType) {
            case Constants.OrderModuleType.SPECIAL:
                OrderPoolEntity orderPool = orderPoolRepository.findByIdOrder(idOrder);
                if (orderPool == null)
                    throw new BusinessException(5002, "订单不存在，请检查参数idOrder是否正确");

                this.finishExternalOrder(orderPool, idOrder);

                //更新任务信息
                jobService.finishOrder(orderPool);
                if (orderPool.getPaymentStatus() == Constants.PaymentStatus.ON_PAY) {
                    status = Constants.OrderStatus.ORDER_STATUS_PAY_COMPLETED;
                }
                //更新订单信息
                if (orderPool.getOrderDistance() == null)
                    orderPool.setOrderDistance(orderPool.getReferenceDistance());
                if (orderPool.getOrderTime() == null)
                    orderPool.setOrderTime(orderPool.getEstimatedTime());
                orderPool.setStatus(status);
                orderPoolRepository.saveAndFlush(orderPool);

                //添加订单状态变化历史
                SpCarOrderStatusHistoryEntity spOrderStatus = new SpCarOrderStatusHistoryEntity();
                spOrderStatus.setIdOrder(idOrder);
                spOrderStatus.setStatus(status);
                spOrderStatus.setCreateBy(Constants.Def_CreateBy);
                spOrderStatus.setMemo(String.format(MessageTemplate.OrderStatus.DRIVER_END_SERVICE, idOrder));
                orderStatusHistoryRepository.save(spOrderStatus);

                result = true;
                break;
            case Constants.OrderModuleType.INTERCITY:
                IntercityOrderEntity icOrder = icOrderRepository.findByIdOrder(idOrder);
                if (icOrder == null)
                    throw new BusinessException(5002, "订单不存在，请检查参数idOrder是否正确");

                this.finishExternalOrder(icOrder, idOrder);

                //更新任务信息
                jobService.finishOrder(icOrder);

                if (icOrder.getPaymentStatus() == Constants.PaymentStatus.ON_PAY) {
                    status = Constants.OrderStatus.ORDER_STATUS_PAY_COMPLETED;
                }

                //更新订单信息
                icOrder.setStatus(status);
                icOrderRepository.saveAndFlush(icOrder);

                //添加订单状态变化历史
                IntercityOrderStatusHistoryEntity icStatus = new IntercityOrderStatusHistoryEntity();
                icStatus.setIdOrder(idOrder);
                icStatus.setStatus(status);
                icStatus.setCreateBy(Constants.Def_CreateBy);
                icStatus.setMemo(String.format(MessageTemplate.OrderStatus.DRIVER_END_SERVICE, idOrder));
                icOrderStatusHistoryRepository.save(icStatus);
                result = true;
                break;
            case Constants.OrderModuleType.EXPRESS:
                ExpressOrderEntity exOrder = exOrderRepository.findByIdOrder(idOrder);
                if (exOrder == null)
                    throw new BusinessException(5002, "订单不存在，请检查参数idOrder是否正确");

                this.finishExternalOrder(exOrder, idOrder);

                //更新任务信息
                jobService.finishOrder(exOrder);

                if (exOrder.getPaymentStatus() == Constants.PaymentStatus.ON_PAY) {
                    status = Constants.OrderStatus.ORDER_STATUS_PAY_COMPLETED;
                }

                //更新订单信息
                exOrder.setStatus(status);
                exOrderRepository.saveAndFlush(exOrder);

                //添加订单状态变化历史
                ExpressOrderStatusHistoryEntity exStatus = new ExpressOrderStatusHistoryEntity();
                exStatus.setIdOrder(idOrder);
                exStatus.setStatus(status);
                exStatus.setCreateBy(Constants.Def_CreateBy);
                exStatus.setMemo(String.format(MessageTemplate.OrderStatus.DRIVER_END_SERVICE, idOrder));
                exOrderStatusHistoryRepository.save(exStatus);
                result = true;
                break;
        }

        return result;
    }

    @Override
    public Boolean cancelOrder(String idOrder, String operator) {
        boolean result = false;
        if (operator==null){
            operator = Constants.Def_CreateBy;
        }
        int moduleType = StringUtil.orderType(idOrder);
        switch (moduleType) {
            case Constants.OrderModuleType.SPECIAL:
                orderPoolBiz.cancelPoolOrder(idOrder);
                result=true;
                break;
            case Constants.OrderModuleType.INTERCITY:
                result = icOrderBiz.cancelOrder(idOrder);
                break;
            case Constants.OrderModuleType.EXPRESS:
                result = exOrderBiz.cancelOrder(idOrder,operator);
                break;
        }
        return result;
    }

    @Override
    public PagingResultVO<OrderForDispatchVO> getOrderDispatchList(OrderForDispatchParam param) {

        //
        String idFranchiseeArr = franchiseeBiz.findIdFranArrByDispacther(param.getIdDispatcher());
        if (StringUtil.isNotBlank(idFranchiseeArr)) {
            param.setIdFranchiseeArr(idFranchiseeArr);
            param.setIdFranchisee(null);
        }

        param.setAppointmentTimeF(DateUtil.getYestodayLocalDate());
        if (param.getHourRange()==null){
            param.setHourRange(Constants.OrderFastSearchType.ALl);
        }
        switch (param.getHourRange()) {
            case Constants.OrderFastSearchType.ALl:
                break;
//            case Constants.OrderModuleType.EXPRESS:
//                break;
            default:
                LocalDateTime appointmentTimeE = DateUtil.getXHoursLaterDateTime(param.getHourRange());
                param.setAppointmentTimeE(cn.hutool.core.date.DateUtil.format(appointmentTimeE,Constants.DATE_FORMAT));
                param.setAppointmentTimeF(DateUtil.getNowLocalDate());;
                break;
        }
        return new PagingResultVO<>(orderMapper.searchWithConditionsAndPagingTotal(param), orderMapper.searchWithConditionsAndPaging(param));
    }

    @Override
    public Long findUnDispatchVirtualOrder(String idFranchisee) {
        return orderMapper.findUnDispatchVirtualOrder(idFranchisee);
    }

    @Override
    public OrderVO virtualAcceptOrder(DispatchParam param) {
        if (StringUtil.isBlank(param.getIdOrder()))
            throw new BusinessException(4001, "订单号idOrder不能为空");

        return this.virtualAcceptOrder(param.getIdOrder(), param.getIdDriver());
    }

    @Override
    public List<ExportOrderVO> findAllExportOrderListData(OrderExportParam param) {
//        if (param.getModuleType()==null)
//        {
//            param.setModuleType(9);
//        }
//        if (StringUtil.isBlank(param.getCreatTimeF())){
//            param.setCreatTimeF(DateUtil.getThisMonthFirstDay());
//        }
        String idFranchiseeArr = franchiseeBiz.findIdFranArrByFinance(param.getIdUser());
        if (StringUtil.isNotBlank(idFranchiseeArr)) {
            param.setIdFranchiseeArr(idFranchiseeArr);
            param.setIdFranchisee(null);
        } else {
            idFranchiseeArr = franchiseeBiz.findIdFranArrByDispacther(param.getIdUser());
            if (StringUtil.isNotBlank(idFranchiseeArr)) {
                param.setIdFranchiseeArr(idFranchiseeArr);
                param.setIdFranchisee(null);
            }
        }
        List<ExportOrderVO> list = orderMapper.findExportOrderListData3(param);
        return list;
    }

    @Override
    public List<ExportOrderVO> findExportOrderListDataWhenAccountMatch(OrderExportParam param) {

        List<ExportOrderVO> list = orderMapper.findExportOrderListDataWhenAccountMatch(param);
        return list;
    }

    @Override
    public PagingResultVO<ExportOrderVO> findExportOrderPageListData(OrderExportParam param) {
//        if (param.getModuleType()==null)
//        {
//            param.setModuleType(9);
//        }

//        if (StringUtil.isBlank(param.getCreatTimeF())) {
//            param.setCreatTimeF(DateUtil.getThisMonthFirstDay());
//        }

        String idFranchiseeArr = franchiseeBiz.findIdFranArrByFinance(param.getIdUser());
        if (StringUtil.isNotBlank(idFranchiseeArr)) {
            param.setIdFranchiseeArr(idFranchiseeArr);
            param.setIdFranchisee(null);
        } else {
            idFranchiseeArr = franchiseeBiz.findIdFranArrByDispacther(param.getIdUser());
            if (StringUtil.isNotBlank(idFranchiseeArr)) {
                param.setIdFranchiseeArr(idFranchiseeArr);
                param.setIdFranchisee(null);
            }
        }

        //log.info("findExportOrderPageListData OrderExportParam : {}", JSON.toJSONString(param));
        List<ExportOrderVO> lst = orderMapper.findExportOrderDataByPage2(param);
        PagingResultVO<ExportOrderVO> vo = new PagingResultVO(orderMapper.findExportOrderDataCount2(param), lst);
        //log.info("ExportOrderVO : {}", JSON.toJSONString(lst));

        for (int i = 0; i < vo.getResults().size(); i++) {
            vo.getResults().get(i).setStatusDsp(Constants.Order_Status.get(vo.getResults().get(i).getStatus()));
            vo.getResults().get(i).setOrderSourceDsp(Constants.Order_Source.get(vo.getResults().get(i).getOrderSource()));
            vo.getResults().get(i).setPaymentTypeDsp(Constants.Payment_Type.get(vo.getResults().get(i).getPaymentType()));
            vo.getResults().get(i).setCityName(HashMapConstants.idFranchiseeCityMap.get(vo.getResults().get(i).getIdFranchisee()));
        }

        return vo;
    }

    @Override
    public PagingResultVO<OrderForDispatchVO> dispatherPendingOrderList(DispatcherOrderParam param) {
        //取得调度员管理的运力商城市
        //List<String> idFranchiseeArr =;
        OrderForDispatchParam orderForDispatchParam = OrderConvertor.INSTANCE.convert2Param(param);
        //查找调度员关联的运营商
        List<String> ids = new ArrayList<>();
        orderForDispatchParam.setIdFranchisee(null);
        orderForDispatchParam.setIdFranchiseeArr(ids.toString());
        return new PagingResultVO<>(orderMapper.searchWithConditionsAndPagingTotal(orderForDispatchParam),
                orderMapper.searchWithConditionsAndPaging(orderForDispatchParam));
    }

    //todo 强制完单调用外部接口
    private void finishExternalOrder(BaseOrderEntity order,String idOrder){
        switch (order.getOrderSource()) {
            case Constants.OrderSource.ALITRIP:
                AlitripCarOrderCompleteRequest.OrderComplete orderComplete = new AlitripCarOrderCompleteRequest.OrderComplete();
                orderComplete.setOrderId(order.getExternalOrderId());
                orderComplete.setThirdOrderId(idOrder);
                orderComplete.setCompleteTime(DateUtil.getNowLocalDate());
                if (order.getOrderDistance() != null && order.getOrderDistance() > 0) {
                    orderComplete.setDistance(order.getOrderDistance().toString());
                } else {
                    orderComplete.setDistance(order.getReferenceDistance().toString());
                }
                AlitripCarOrderCompleteRequest.PriceInfo priceInfo = new AlitripCarOrderCompleteRequest.PriceInfo();
                priceInfo.setTotalPrice(order.getOrderMoney().toString());
                orderComplete.setPriceInfo(priceInfo);
                orderComplete.setProviderId(AliTripTransferConstants.ProviderIdWithMemberID.get(order.getIdMember()));
                String tenant = AliTripTransferConstants.MemberIDFindMerchant.get(order.getIdMember());
                AlitripCarOrderCompleteResponse res = alitripCarOpen.orderComplete(orderComplete, tenant);
                if (res == null || res.getMessage() == null
                        || !(res.getMessage().equals("成功") || res.getMessage().toLowerCase(Locale.ROOT).equals("ok")))
                    throw new BusinessException(5002, "飞猪接口回调失败，请重新执行");
                break;

            case Constants.OrderSource.TONGCHENG_RIDE:
                if (order.getStatus() < Constants.OrderStatus.ORDER_STATUS_IN_SERVICE
                        || order.getStatus() > Constants.OrderStatus.ORDER_STATUS_WAITING_TO_PAY)
                    throw new BusinessException(5002, "同程订单在未接到乘客或者已取消状态下不能强制完单");

                String driverInfo = cacheRedis.getOrderDriverValue(idOrder);
                DriverInfoVO vo = null;
                if (StringUtil.isNotBlank(driverInfo)) {
                    vo = JSON.parseObject(driverInfo, DriverInfoVO.class);
                } else {
                    vo = driverService.driverInfo(order.getIdDriver());
                }

                if (vo == null)
                    throw new BusinessException(5002, "司机资料不完善");

                OrderStatusDTO dto = (OrderStatusDTO) new OrderStatusDTO()
                        .setStatus(TongChengConstants.ELong_OrderType.get(Constants.OrderStatus.ORDER_STATUS_PAY_COMPLETED))
                        .setDriverName(vo.getRealName())
                        .setDriverPhone(vo.getDriverMobile())
                        .setVehiclePlateNum(vo.getPlateNumber())
                        .setVehicleColor(vo.getColor())
                        .setVehicleModel(vo.getModel())
                        .setVehicleBrand(vo.getBrand())
                        ;
                dto.setOrderId(order.getExternalOrderId());
                dto.setSupplierOrderId(idOrder);

                String idChannel = authService.findIdChannel(null, order.getIdFranchisee());
                if (StringUtil.isBlank(idChannel))
                    throw new BusinessException(5002, "未找到对应的同程渠道，请检查渠道ID");

                ResultVO resultVO = callback.orderStatus(idChannel, dto);
                if (resultVO == null || resultVO.getStatus() != 200)
                    throw new BusinessException(5002, "同程接口回调失败，请重新执行");

                break;
        }
    }

//    /**
//     * 司机派单
//     * @param param
//     * @return
//     */
//    @Transactional
//    @Override
//    public Boolean dispatchDriver(DispatchDriverParam param) {
//        boolean result = false;
//        switch (param.getModuleType()) {
//            case Constants.OrderModuleType.SPECIAL:
//                result = orderPoolBiz.dispatchDriver(param);
//                break;
//            case Constants.OrderModuleType.INTERCITY:
//                result = icOrderBiz.dispatchDriver(param);
//                break;
//            case Constants.OrderModuleType.EXPRESS:
//                result = exOrderBiz.dispatchDriver(param);
//                break;
//        }
//        return result;
//    }

    private OrderVO virtualAcceptOrder(String idOrder, String idDriver) {
        int moduleType = StringUtil.orderType(idOrder);
        OrderVO vo = new OrderVO();

        switch (moduleType) {
            case Constants.OrderModuleType.SPECIAL:
                vo = orderPoolBiz.virtualAcceptOrder(idOrder,idDriver);
                break;
            case Constants.OrderModuleType.INTERCITY:
                vo = icOrderBiz.virtualAcceptOrder(idOrder,idDriver);
                break;
            case Constants.OrderModuleType.EXPRESS:
                vo = exOrderBiz.virtualAcceptOrder(idOrder,idDriver);
                break;
        }

        return vo;
    }

}
